<html lang="en">
	<head>
		<title>Yuka | Trigger</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link rel="stylesheet" type="text/css" href="../../lib/styles.css">
		<link rel="shortcut icon" type="image/x-icon" href="https://mugen87.github.io/yuka/favicon.ico">
	</head>
<body>

	<section id="info">
		<p>
			The game entity's color changes to green when it enters a trigger region.
		</p>
	</section>

	<script type="module">

	import * as YUKA from '../../../build/yuka.module.js';
	import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.109/build/three.module.js';

	import { CustomTrigger } from './src/CustomTrigger.js';

	let renderer, scene, camera;

	let entityManager, time, entity;

	init();
	animate();

	function init() {

		scene = new THREE.Scene();

		camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 1000 );
		camera.position.set( 0, 10, 15 );
		camera.lookAt( scene.position );

		//

		const entityGeometry = new THREE.BoxBufferGeometry( 0.5, 0.5, 0.5 );
		const entityMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );

		const entityMesh = new THREE.Mesh( entityGeometry, entityMaterial );
		entityMesh.matrixAutoUpdate = false;
		scene.add( entityMesh );

		const grid = new THREE.GridHelper( 10, 25 );
		scene.add( grid );

		// game entity setup

		entityManager = new YUKA.EntityManager();
		time = new YUKA.Time();

		entity = new YUKA.GameEntity();
		entity.boundingRadius = 0.25;
		entity.setRenderComponent( entityMesh, sync );

		entityManager.add( entity );

		const radius = 2;
		const size = new YUKA.Vector3( 3, 3, 3 );

		const sphericalTriggerRegion = new YUKA.SphericalTriggerRegion( radius );
		const rectangularTriggerRegion = new YUKA.RectangularTriggerRegion( size );

		const trigger1 = new CustomTrigger( sphericalTriggerRegion );
		trigger1.position.set( 3, 0, 0 );

		const trigger2 = new CustomTrigger( rectangularTriggerRegion );
		trigger2.position.set( - 3, 0, 0 );

		entityManager.add( trigger1 );
		entityManager.add( trigger2 );

		// visualize triggers

		const sphereGeometry = new THREE.SphereBufferGeometry( radius, 16, 16 );
		const sphereMaterial = new THREE.MeshBasicMaterial( { color: 0x6083c2, wireframe: true } );
		const triggerMesh1 = new THREE.Mesh( sphereGeometry, sphereMaterial );
		triggerMesh1.matrixAutoUpdate = false;
		scene.add( triggerMesh1 );

		trigger1.setRenderComponent( triggerMesh1, sync );

		const boxGeometry = new THREE.BoxBufferGeometry( size.x, size.y, size.z );
		const boxMaterial = new THREE.MeshBasicMaterial( { color: 0x6083c2, wireframe: true } );
		const triggerMesh2 = new THREE.Mesh( boxGeometry, boxMaterial );
		triggerMesh2.matrixAutoUpdate = false;
		scene.add( triggerMesh2 );

		trigger2.setRenderComponent( triggerMesh2, sync );

		// renderer

		renderer = new THREE.WebGLRenderer( { antialias: true } );
		renderer.setPixelRatio( window.devicePixelRatio );
		renderer.setSize( window.innerWidth, window.innerHeight );
		document.body.appendChild( renderer.domElement );

		//

		window.addEventListener( 'resize', onWindowResize, false );

	}

	function onWindowResize() {

		camera.aspect = window.innerWidth / window.innerHeight;
		camera.updateProjectionMatrix();

		renderer.setSize( window.innerWidth, window.innerHeight );

	}

	function animate() {

		requestAnimationFrame( animate );

		const delta = time.update().getDelta();
		const elapsedTime = time.getElapsed();

		entity.position.x = Math.sin( elapsedTime ) * 2;
		entity._renderComponent.material.color.set( 0xff0000 ); // reset color

		entityManager.update( delta );

		renderer.render( scene, camera );

	}

	function sync( entity, renderComponent ) {

		renderComponent.matrix.copy( entity.worldMatrix );

	}

	</script>

</body>
</html>
